﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RclSystem
{
    public partial class SaleStatementDetail_Company : UIForm
    {
        private string salesman = "";
        private string companyname = "";
        private string sdate = "";
        private string edate = "";
        private string presdate = "";
        List<StatementAccountVo> sortedVolist = new List<StatementAccountVo>();
        public SaleStatementDetail_Company(string salesman,string companyname,string sdate,string edate)
        {
            InitializeComponent();
            this.salesman = salesman;
            this.companyname = companyname;
            this.sdate = sdate;
            this.edate = edate;
            this.Text = "业务员对账明细[" + salesman + "-" + companyname + "]";
            presdate = DateTime.Parse(sdate).AddDays(-1).ToString("yyyy-MM-dd");
        }

        //统计查询前一天的期初数
        private StatementAccountVo tjinitial()
        {
            //期初数结算金额
            double initaljs = 0;
            List<StatementAccountVo> volist = new List<StatementAccountVo>();

            //获取发货单列表
            DataTable dt_deliver = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_delivery where salesman = '" + salesman + "' and companyname = '" + this.companyname
                + "' and deliverdate <= '" + presdate + "' order by deliverdate");

            if (dt_deliver.Rows.Count > 0)
            {
                foreach (DataRow row in dt_deliver.Rows)
                {
                    initaljs += double.Parse(row["saleamount"].ToString());
                }
            }

            //获取收款单列表
            DataTable dt_receipt = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_receipt where salesman = '" + salesman + "' and companyname = '"
                + this.companyname  + "' and receiptdate <= '" + presdate + "'");
            
            if (dt_receipt.Rows.Count > 0)
            {
                foreach (DataRow row in dt_receipt.Rows)
                {
                    if (row["parentid"].ToString() != "0")
                    {
                        initaljs -= double.Parse(row["receiptamount"].ToString());
                    }                 
                    
                }
            }

            //判断是否有期初数
            //获取单位信息
            DataTable dt_company = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_company where name = '" + this.companyname + "'");
            if (dt_company.Rows.Count > 0)
            {
                foreach (DataRow row in dt_company.Rows)
                {
                    initaljs += double.Parse(row["initalamount"].ToString());
                }
            }

            //添加总期初数行
            StatementAccountVo initalvo = new StatementAccountVo();
            initalvo.companyname = "期初数";
            initalvo.optdate = DateTime.Parse(presdate);
            initalvo.deliverprice = Math.Round(initaljs, 2);
            return initalvo;
        }

        private void queryAccounts()
        {
            sortedVolist = new List<StatementAccountVo>();
            uiDataGridView1.Rows.Clear();
            List<StatementAccountVo> volist = new List<StatementAccountVo>();

            //获取发货单列表
            DataTable dt_deliver = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_delivery where salesman = '" + salesman + "' and companyname = '" + this.companyname 
                + "' and deliverdate >= '" + sdate + "' and deliverdate <= '" + edate + "' order by deliverdate");

            if (dt_deliver.Rows.Count > 0)
            {
                foreach (DataRow row in dt_deliver.Rows)
                {
                    StatementAccountVo vo = new StatementAccountVo();
                    vo.companyname = row["companyname"].ToString();
                    vo.optdate = (DateTime)row["deliverdate"];
                    vo.count = double.Parse(row["salecount"].ToString());
                    vo.deliverprice = double.Parse(row["saleamount"].ToString());
                    vo.balance += double.Parse(row["saleamount"].ToString());
                    volist.Add(vo);
                }
            }

            //获取收款单列表
            DataTable dt_receipt = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_receipt where salesman = '" + salesman + "' and companyname = '" 
                + this.companyname + "' and  receiptdate >= '" + sdate + "' and receiptdate <= '" + edate + "'");
            //期初数结算金额
            //double initaljs = 0;
            if (dt_receipt.Rows.Count > 0)
            {
                foreach (DataRow row in dt_receipt.Rows)
                {
                    StatementAccountVo vo = new StatementAccountVo();
                    vo.companyname = row["companyname"].ToString();
                    vo.optdate = (DateTime)row["receiptdate"];
                    vo.receiptprice = double.Parse(row["receiptamount"].ToString());
                    vo.balance -= double.Parse(row["receiptamount"].ToString());
                    if (row["parentid"].ToString() == "0")
                    {
                        //initaljs += vo.receiptprice;
                    }
                    volist.Add(vo);
                }
            }

            //判断是否有期初数
            //获取单位信息
            /*DataTable dt_company = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_company where name = '" + this.companyname + "'");
            if (dt_company.Rows.Count > 0)
            {
                foreach (DataRow row in dt_company.Rows)
                {
                    initaljs += double.Parse(row["initalamount"].ToString());
                }
            }

            //添加总期初数行
            StatementAccountVo initalvo = new StatementAccountVo();
            initalvo.companyname = "期初数";
            initalvo.optdate = DateTime.Parse("0001-01-01");
            initalvo.deliverprice = initaljs;
            volist.Add(initalvo);*/
            volist.Add(tjinitial());
            //对清单进行排序-按日期升序
            sortedVolist = volist.OrderBy(p => p.optdate).ToList();
             //显示
            int index = 0;
            double count_total = 0;
            double deliverprice_total = 0;
            double receiptprice_total = 0;
            foreach (StatementAccountVo vo in sortedVolist)
            {
                index = this.uiDataGridView1.Rows.Add();
                this.uiDataGridView1.Rows[index].Cells[0].Value = index + 1;
                this.uiDataGridView1.Rows[index].Cells[1].Value = vo.companyname;
                this.uiDataGridView1.Rows[index].Cells[2].Value = vo.optdate.ToString("yyyy-MM-dd") == presdate ? presdate : vo.optdate.ToString("yyyy-MM-dd");
                this.uiDataGridView1.Rows[index].Cells[3].Value = vo.count.ToString() == "0" ? "" : vo.count.ToString();
                this.uiDataGridView1.Rows[index].Cells[4].Value = vo.deliverprice.ToString() == "0" ? "" : vo.deliverprice.ToString();
                this.uiDataGridView1.Rows[index].Cells[5].Value = vo.receiptprice.ToString() == "0" ? "" : vo.receiptprice.ToString();

                count_total += vo.count;
                deliverprice_total += vo.deliverprice;
                receiptprice_total += vo.receiptprice;
            }
            //生成合计行
            index = this.uiDataGridView1.Rows.Add();
            this.uiDataGridView1.Rows[index].Cells[0].Value = index + 1;
            this.uiDataGridView1.Rows[index].Cells[1].Value = "合计";
            this.uiDataGridView1.Rows[index].Cells[2].Value = "";
            this.uiDataGridView1.Rows[index].Cells[3].Value = Math.Round(count_total, 3).ToString();
            this.uiDataGridView1.Rows[index].Cells[4].Value = Math.Round(deliverprice_total, 2).ToString();
            this.uiDataGridView1.Rows[index].Cells[5].Value = Math.Round(receiptprice_total, 2).ToString();
            
        }

        private void SaleStatementDetail_Company_Load(object sender, EventArgs e)
        {
            queryAccounts();
        }
    }
}
